﻿using JESAI.Framework.Filters;
using JESAI.MonacoRoslynComplete.Models;
using JESAI.MonacoRoslynComplete.UI.Dtos;
using JESAI.MonacoRoslynConplete.Storages;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace JESAI.MonacoRoslynComplete.UI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [IgnoreResultHandlerAttribute]
    public class CodeFileController : ControllerBase
    {
        private readonly IMonacoRoslynCompleteStorage _monacoRoslynCompleteStorage;

        public CodeFileController(IMonacoRoslynCompleteStorage monacoRoslynCompleteStorage)
        {
            _monacoRoslynCompleteStorage = monacoRoslynCompleteStorage;
        }

        [HttpPost("AddCodeFile")]
        public async Task<IActionResult> AddCodeFile([FromBody] CodeFileInput input)
        {
            ICodeFileModel codeFile = input.Map<CodeFileModel>();
            codeFile.CreateTime = DateTime.Now;
            codeFile.IsDeleted = false;
            await _monacoRoslynCompleteStorage.AddCodeFileAsync(codeFile);
            return Ok(new { code = 0, message = "success" });
        }

        [HttpPost("UpdateCodeFile")]
        public async Task<IActionResult> UpdateCodeFile([FromBody] CodeFileInput input)
        {
            ICodeFileModel codeFile = input.Map<CodeFileModel>();
            await _monacoRoslynCompleteStorage.UpdateCodeFileAsync(codeFile);
            return Ok(new { code = 0, message = "success" });
        }

        [HttpGet("DeleteCodeFile")]
        public async Task<IActionResult> DeleteCodeFile(string id)
        {
            await _monacoRoslynCompleteStorage.DeleteCodeFile(id);
            return Ok(new { code = 0, message = "success" });
        }

        [HttpGet("MoveFileOut")]
        public async Task<IActionResult> MoveFileOut(string id)
        {
            await _monacoRoslynCompleteStorage.MoveFileOut(id);
            return Ok(new { code = 0, message = "success" });
        }

        [HttpPost("AddCodeFileContent")]
        public async Task<IActionResult> AddCodeFileContent([FromBody] CodeFileContentInput input)
        {
            ICodeFileContentModel codeFileContent = input.Map<CodeFileContentModel>();
            codeFileContent.CreateTime = DateTime.Now;
            codeFileContent.IsValid = true;
            await _monacoRoslynCompleteStorage.AddCodeContentAsync(codeFileContent);
            return Ok(new { code = 0, message = "success" });
        }

        [HttpGet("GetAllCodeFileDatas")]
        public async Task<IActionResult> GetAllCodeFileDatas()
        {
            var datas = await _monacoRoslynCompleteStorage.GetAllCodeFileDatas();
            return Ok(new { code = 0, message = "success", data = datas });
        }

        [HttpGet("GetCodeFileContent")]
        public async Task<IActionResult> GetCodeFileContent(string codeFileTreeId)
        {
            var data = await _monacoRoslynCompleteStorage.GetCodeFileContent(codeFileTreeId);
            return Ok(new { code = 0, message = "success", data = data });
        }
    }
}
